//
//  CalculateLabelHeight.m
//  TestSuZhou
//
//  Created by 刘应平 on 16/7/15.
//  Copyright © 2016年 刘应平. All rights reserved.
//

#import "CalculateLabelHeight.h"

@implementation CalculateLabelHeight

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

+ (CGSize)CalculateLabelHeightWithString:(NSString *)string font:(CGFloat)font width:(CGFloat)width{
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                       options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:font]}
                                       context:nil].size;
    NSString *str = @"1";
    CGSize size1 = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:font]}
                                     context:nil].size;
    //数据转string 去掉多余的空格特殊符号 然后转data 转字典
    NSInteger number = 0;
    /**  只需要判断末尾是否有换行  */
    if ([string hasSuffix:@"\n"]) {
        number ++;
    }
    size.height += number * size1.height;
    return size;
}

+ (CGSize)CalculateLabelWidthWithString:(NSString *)string font:(CGFloat)font height:(CGFloat)height{
    CGSize size = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height)
                                       options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:font]}
                                       context:nil].size;
    
    return size;
    
}


+ (CGFloat)calculateLabeHeightWithString:(NSString *)string font:(CGFloat)font width:(CGFloat)width space:(CGFloat)space{
    CGFloat height1 = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                       options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:font]}
                                       context:nil].size.height;
    NSString *str = @"你好";
    CGFloat height2 = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:font]}
                                     context:nil].size.height;
    //数据转string 去掉多余的空格特殊符号 然后转data 转字典
    NSInteger number = 0;
    /**  只需要判断末尾是否有换行  */
    if ([string hasSuffix:@"\n"]) {
        number ++;
    }
    height1 += number * height2;
    
    /**  设置行距  */
    NSInteger row = height1 / height2;
    height1 += (row - 1) * space;
    
    return height1;
}


/*
 计算UILabel的高度(带有行间距的情况)
 (NSString*)str  内容
 withFont:(float)font 字体大小
 WithSpace:(float)space 行间距
 (CGFloat)width  UILable的宽度
 */
//计算UILabel的高度(带有行间距的情况)
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(float)font withWidth:(CGFloat)width WithSpace:(float)space{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = space;
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    UIFont *tfont = [UIFont systemFontOfSize:font];
    NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return size.height;
}


@end
